package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.CmsCategory;
import com.example.demo.common.Response;
import com.example.demo.service.CmsCategoryService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("/category")
public class CmsCategoryController {
	@Autowired
	CmsCategoryService categoryService;

	@GetMapping("/Bypid")
	public Response<List<CmsCategory>> allCategoryByPid(){
		List<CmsCategory> list = categoryService.allCategoryByPid();
		return Response.ok(list);
	}
	
	@PostMapping("/insert")
	public Response<String> insertData(CmsCategory category){
		categoryService.insert(category);
		return Response.ok("success");
	}
	
	@ApiOperation(value = "按照id删除")
	@ApiImplicitParam(name="id",value="学号",dataType="string",required=true)
	@DeleteMapping("/deleteById")
	public Response<String> deleteByIds(String id){
		int count = categoryService.deleteById(id);
		return Response.ok("删除成功"+count+"条栏目!");
	}
	
	@ApiOperation(value = "按id修改表中数据")
	@PostMapping("/saveOrUpdate")
	public Response<String> saveOrUpdate(CmsCategory category){
		categoryService.saveOrUpdate(category);
		return Response.ok("操作成功!");
	}
	
	@ApiOperation(value = "列出表中所有数据")
	@GetMapping("/All")
	public Response<List<CmsCategory>> findAll(){
		List<CmsCategory> all = categoryService.findAll();
		return Response.ok(all);
	}
	
	@ApiOperation(value = "按id来找出表中数据")
	@GetMapping("/select")
	public Response<CmsCategory> selectById(String id){
		CmsCategory data = categoryService.selectById(id);
		return Response.ok(data);
	}
}
